package com.freight.core.config.security.permission;

import cn.hutool.core.util.StrUtil;
import com.freight.core.common.constant.CommonConstant;
import com.freight.core.common.constant.SecurityConstant;
import com.freight.core.common.redis.RedisTemplateHelper;
import com.freight.core.common.utils.ResponseUtil;
import com.freight.core.config.properties.CaptchaProperties;
import com.freight.core.entity.User;
import com.freight.core.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.PathMatcher;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户登录角色过滤
 *
 * @describe
 */
@Slf4j
@Configuration
public class UserRoleFilter extends OncePerRequestFilter {

    @Autowired
    private CaptchaProperties captchaProperties;

    @Autowired
    private RedisTemplateHelper redisTemplate;

    @Autowired
    private PathMatcher pathMatcher;

    @Autowired
    private UserService userService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {

        // 判断URL是否需要验证
        Boolean flag = false;
        String requestUrl = request.getRequestURI();

        if (pathMatcher.match("/freight/login", requestUrl)) {
            flag = true;
        }

        if (flag) {
            // 登入类型
            String lognType = request.getParameter("loginType");
            String username = request.getParameter("username");
            if(username != null || !username.equals("")){


            if (lognType == null) {
                lognType = "1";
            }

            User user = userService.findByUsername(username);
            if(user!=null){
                if (lognType.equals(SecurityConstant.OTHER_LOGIN)) {
                    if (user.getUserType().equals(CommonConstant.USER_TYPE_ADMIN)) {
                        ResponseUtil.out(response,
                                ResponseUtil.resultMap(false, 500,
                                        "管理员账户不能登录"));
                        return;
                    }
                    if (user.getUserType().equals(CommonConstant.USER_TYPE_COMPANY)) {
                        ResponseUtil.out(response,
                                ResponseUtil.resultMap(false, 500,
                                        "货主账户不能登录"));
                        return;
                    }
                }

                if (lognType.equals(SecurityConstant.PC_LOGIN)) {

                    if (user.getUserType().equals(CommonConstant.USER_TYPE_DRIVER)) {
                        ResponseUtil.out(response,
                                ResponseUtil.resultMap(false, 500,
                                        "司机账户不能登录"));
                        return;
                    }
                }
            }
            }

        }
        // 无需验证 放行
        chain.doFilter(request, response);
    }
}
